# PBJ Server Makefile

# 变量定义
APP_NAME = pbj-server
BUILD_DIR = build
MAIN_FILE = main.go
CONFIG_FILE = config/config.yaml

# Go 相关变量
GOCMD = go
GOBUILD = $(GOCMD) build
GOCLEAN = $(GOCMD) clean
GOTEST = $(GOCMD) test
GOGET = $(GOCMD) get
GOMOD = $(GOCMD) mod
BINARY_NAME = $(APP_NAME)
BINARY_UNIX = $(BINARY_NAME)_unix

# 默认目标
.PHONY: all
all: clean build

# 安装依赖
.PHONY: deps
deps:
	$(GOMOD) download
	$(GOMOD) tidy

# 构建应用
.PHONY: build
build:
	$(GOBUILD) -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_FILE)

# 构建 Linux 版本
.PHONY: build-linux
build-linux:
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BUILD_DIR)/$(BINARY_UNIX) $(MAIN_FILE)

# 构建 Windows 版本
.PHONY: build-windows
build-windows:
	CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(BUILD_DIR)/$(BINARY_NAME).exe $(MAIN_FILE)

# 构建 macOS 版本
.PHONY: build-macos
build-macos:
	CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(BUILD_DIR)/$(BINARY_NAME)_macos $(MAIN_FILE)

# 运行应用
.PHONY: run
run:
	$(GOCMD) run $(MAIN_FILE)

# 运行并监听文件变化（需要安装 air）
.PHONY: dev
dev:
	air

# 清理构建文件
.PHONY: clean
clean:
	$(GOCLEAN)
	rm -rf $(BUILD_DIR)
	rm -f $(BINARY_NAME)
	rm -f $(BINARY_NAME).exe

# 运行测试
.PHONY: test
test:
	$(GOTEST) -v ./...

# 运行测试并生成覆盖率报告
.PHONY: test-coverage
test-coverage:
	$(GOTEST) -v -coverprofile=coverage.out ./...
	$(GOCMD) tool cover -html=coverage.out -o coverage.html

# 格式化代码
.PHONY: fmt
fmt:
	$(GOCMD) fmt ./...

# 代码检查
.PHONY: lint
lint:
	golangci-lint run

# 安装代码检查工具
.PHONY: install-lint
install-lint:
	go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

# 安装热重载工具
.PHONY: install-air
install-air:
	go install github.com/cosmtrek/air@latest

# 生成 API 文档
.PHONY: swagger
swagger:
	swag init -g $(MAIN_FILE)

# 安装 Swagger 工具
.PHONY: install-swagger
install-swagger:
	go install github.com/swaggo/swag/cmd/swag@latest

# Docker 相关命令
.PHONY: docker-build
docker-build:
	docker build -t $(APP_NAME) .

.PHONY: docker-run
docker-run:
	docker run -p 8080:8080 $(APP_NAME)

.PHONY: docker-stop
docker-stop:
	docker stop $(APP_NAME) || true
	docker rm $(APP_NAME) || true

# Docker Compose 命令
.PHONY: compose-up
compose-up:
	docker-compose up -d

.PHONY: compose-down
compose-down:
	docker-compose down

.PHONY: compose-logs
compose-logs:
	docker-compose logs -f

.PHONY: compose-restart
compose-restart:
	docker-compose restart

# 数据库相关命令
.PHONY: db-migrate
db-migrate:
	$(GOCMD) run $(MAIN_FILE) migrate

.PHONY: db-seed
db-seed:
	$(GOCMD) run $(MAIN_FILE) seed

# 创建必要的目录
.PHONY: init
init:
	mkdir -p $(BUILD_DIR)
	mkdir -p logs
	mkdir -p docker/mysql
	mkdir -p docker/nginx/ssl

# 安装所有开发工具
.PHONY: install-tools
install-tools: install-lint install-air install-swagger

# 显示帮助信息
.PHONY: help
help:
	@echo "PBJ Server Makefile 命令:"
	@echo ""
	@echo "构建相关:"
	@echo "  build          - 构建应用"
	@echo "  build-linux    - 构建 Linux 版本"
	@echo "  build-windows  - 构建 Windows 版本"
	@echo "  build-macos    - 构建 macOS 版本"
	@echo "  clean          - 清理构建文件"
	@echo ""
	@echo "运行相关:"
	@echo "  run            - 运行应用"
	@echo "  dev            - 开发模式运行（热重载）"
	@echo ""
	@echo "测试相关:"
	@echo "  test           - 运行测试"
	@echo "  test-coverage  - 运行测试并生成覆盖率报告"
	@echo ""
	@echo "代码质量:"
	@echo "  fmt            - 格式化代码"
	@echo "  lint           - 代码检查"
	@echo "  install-lint   - 安装代码检查工具"
	@echo ""
	@echo "文档相关:"
	@echo "  swagger        - 生成 API 文档"
	@echo "  install-swagger - 安装 Swagger 工具"
	@echo ""
	@echo "Docker 相关:"
	@echo "  docker-build   - 构建 Docker 镜像"
	@echo "  docker-run     - 运行 Docker 容器"
	@echo "  docker-stop    - 停止 Docker 容器"
	@echo ""
	@echo "Docker Compose:"
	@echo "  compose-up     - 启动所有服务"
	@echo "  compose-down   - 停止所有服务"
	@echo "  compose-logs   - 查看服务日志"
	@echo "  compose-restart - 重启所有服务"
	@echo ""
	@echo "数据库相关:"
	@echo "  db-migrate     - 数据库迁移"
	@echo "  db-seed        - 数据库种子数据"
	@echo ""
	@echo "工具安装:"
	@echo "  install-tools  - 安装所有开发工具"
	@echo "  install-air    - 安装热重载工具"
	@echo ""
	@echo "其他:"
	@echo "  init           - 初始化项目目录"
	@echo "  deps           - 安装依赖"
	@echo "  help           - 显示此帮助信息" 